#include "slepbase.h"
#include "slepui.h"
#include "cslep.h"
#include <QtGui>


void SLEPUI::actmtLeastR(){
	optsWidget = new OptionsWidget(Method_mtLeast_R);
	LogWidget::instance()->log(QString("Executing Multi-task Lasso"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_matrix* X = slepAllocDenseMatrix(A->n,opts->ind_size - 1);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		mtLeastR(A,y,optsWidget->getL1RegCoefficient(),opts,X,funVal,valueL);
		LogWidget::instance()->log(QString("Success executing Multi-task Lasso"));

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemX = FileOperations::slepMatrix2SlepItem(X);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);
		SLEPItem *itemGroup = new SLEPItem;
		itemGroup->group_index = opts->ind;
		itemGroup->group_size = opts->ind_size;
		itemGroup->type = SLEPItem::GROUP_INDICES;

		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemX,tr("X"));
		writeToCurrentDir(itemFVal,tr("funVal"));
		writeToCurrentDir(itemGroup,tr("groupinfo"));


		slepFreeMatrix(A);
		slepFreeMatrix(X);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}


void SLEPUI::actmtLogisticR(){
	optsWidget = new OptionsWidget(Method_mtLogistic_R);
	LogWidget::instance()->log(QString("Executing Multi-task Logistic Lasso"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_vector *c_v = slepAllocDenseVector( opts->ind_size - 1 );
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_matrix* X = slepAllocDenseMatrix(A->n,opts->ind_size - 1);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		mtLogisticR(A,y,optsWidget->getL1RegCoefficient(),opts,X,c_v,funVal,valueL);
		LogWidget::instance()->log(QString("Success executing Multi-task Logistic Lasso"));

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemX = FileOperations::slepMatrix2SlepItem(X);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);
		SLEPItem *itemc = FileOperations::slepVector2SlepItem(c_v);

		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemX,tr("X"));
		writeToCurrentDir(itemFVal,tr("funVal"));
		writeToCurrentDir(itemc,tr("c"));

		slepFreeMatrix(A);
		slepFreeMatrix(X);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}

/*
void SLEPUI::actmtLogisticR(){
	optsWidget = new OptionsWidget(Method_mtLogistic_R);
	double c;
	LogWidget::instance()->log(QString("Group Lasso with Logistic Loss - Options"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);


		stopWatch timer;
		LogWidget::instance()->log(QString("Now running..."));
		::startTimer(&timer);
		glLogisticR(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal,valueL);
		::stopTimer(&timer);
		LogWidget::instance()->log(QString("It took %1 seconds.").arg(getElapsedTime(&timer)));
		LogWidget::instance()->log(QString("Success executing Group Lasso with Logistic Loss!"));


		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);

		SLEPItem *itemC = new SLEPItem;

		itemC->singlevalue = c;
		itemC->type=SLEPItem::SINGLE_VALUE;
		writeToCurrentDir(itemC,tr("c"));

		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));

		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}
*/

